package com.pkk.cloud.support.oauth2.config.configuration;

import com.pkk.cloud.support.oauth2.config.handle.WebResponseExceptionTranslator;
import com.pkk.cloud.support.oauth2.config.handle.Oauth2LogoutHandler;
import com.pkk.cloud.support.oauth2.config.handle.Oauth2LogoutSuccessHandler;
import com.pkk.cloud.support.oauth2.config.integration.IntegrationAuthenticationFilter;
import com.pkk.cloud.support.oauth2.config.properties.JwtTokenProperties;
import com.pkk.cloud.support.oauth2.store.store.RedisTokenStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfiguration;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.util.Assert;

/**
 * @Description: 配置必要的bean，web资源的配置
 * @return:
 * @Author: peikunkun
 * @Date: 2020/3/21 0021 下午 1:53
 */
@Configuration
@Import({WebSecurityAccessConfiguration.class, ResourceServerConfiguration.class})
public class Oauth2CenterConfiguration {

  @Autowired
  private RedisConnectionFactory redisConnectionFactory;
  @Autowired
  private JwtTokenProperties jwtTokenProperties;

  /**
   * @Description: 设置默认异常响应类响应信息
   * @return: org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator
   * @Author: peikunkun
   * @Date: 2020/3/19 0019 下午 3:30
   */
  @Bean
  public WebResponseExceptionTranslator webResponseExceptionTranslator() {
    return new WebResponseExceptionTranslator();
  }

  /**
   * @Description: 自己根据不同的认证操作去认证
   * @return: com.pkk.cloud.support.oauth2.config.integration.IntegrationAuthenticationFilter
   * @Author: peikunkun
   * @Date: 2020/3/19 0019 下午 3:47
   */
  @Bean
  public IntegrationAuthenticationFilter integrationAuthenticationFilter() {
    return new IntegrationAuthenticationFilter();
  }

  /**
   * @Description: 登出的处理
   * @return: org.springframework.security.web.authentication.logout.LogoutHandler
   * @Author: peikunkun
   * @Date: 2020/3/21 0021 下午 11:04
   */
  @Bean
  public LogoutHandler logoutHandler() {
    return new Oauth2LogoutHandler();
  }

  /**
   * @Description: 注入登录成功的处理业务
   * @return: org.springframework.security.web.authentication.logout.LogoutSuccessHandler
   * @Author: peikunkun
   * @Date: 2020/3/19 0019 下午 2:55
   */
  @Bean
  public LogoutSuccessHandler logoutSuccessHandler() {
    return new Oauth2LogoutSuccessHandler();
  }

  /**
   * @Description: 配置使用redis储存token
   * @return: org.springframework.security.oauth2.provider.token.TokenStore
   * @Author: peikunkun
   * @Date: 2020/3/19 0019 下午 2:57
   */
  @Bean
  public TokenStore redisTokenStore() {
    Assert.notNull(redisConnectionFactory, "redisConnectionFactory must be set");
    return new RedisTokenStore(redisConnectionFactory);
  }

  /**
   * @Description: token的处理转换器
   * @return: org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter
   * @Author: peikunkun
   * @Date: 2020/3/19 0019 下午 2:58
   */
  @Bean
  public JwtAccessTokenConverter jwtAccessTokenConverter() {
    Assert.notNull(jwtTokenProperties, "jwtTokenProperties must be set");
    Assert.notNull(jwtTokenProperties.getTokenSign(), "properties jwt token sign must be set");
    //我们可以使用自己的项目名称jwt.token-sign = ${spring.application.name}
    JwtAccessTokenConverter accessTokenConverter = new JwtAccessTokenConverter();
    accessTokenConverter.setSigningKey(jwtTokenProperties.getTokenSign());
    return accessTokenConverter;
  }
}
